﻿// Used to help read / write data in a byte array

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class Buffer
{
    public byte[] buffer;
    public uint pos = 0;
    public int LastAddress = 0;
    public void ProtectRegion(int start, int Length)
    {

    }
    public byte Read()
    {
        byte val = buffer[pos];
        pos++;
        return val;
    }
    public byte[] getBytes()
    {
        return buffer;
    }
    public int ReadInt32()
    {
        int val = BitConverter.ToInt32(buffer, (int)pos);
        pos += 4;
        return val;
    }
    public short ReadInt16()
    {

        short val = BitConverter.ToInt16(buffer, (int)pos);
        pos += 2;
        return val;
    }
    public short ReadInt16(uint posp)
    {

        short val = BitConverter.ToInt16(buffer, (int)posp);
        pos += 2;
        return val;
    }
    public string ReadString()
    {
        byte length = Read();

        string final = "";
        for (int i = 0; i < length; i++)
        {
            final += (char)Read();

        }
        return final;
    }
    public byte ReadByte(uint Address)
    {
        return buffer[Address];
    }

    public void Write(uint Address, byte Data)
    {
        try
        {
            buffer[Address] = Data;
        }
        catch
        {
            //throw new Exception("Cant write to address:" + Address);
        }
        // LastAddress = (int)Address;
    }
    public void Write(uint Address, string Data)
    {
        Write(Address, (byte)Data.Length);
        Address++;
        foreach (char c in Data)
        {
            buffer[Address] = (byte)c;
            Address++;
        }
        // LastAddress = (int)Address;
    }
    public void Write(uint Address, int Data)
    {

        byte[] Size = BitConverter.GetBytes(Data);
        buffer[Address] = Size[0];
        buffer[Address + 1] = Size[1];
        buffer[Address + 2] = Size[2];
        buffer[Address + 3] = Size[3];
        
      
        Address += 4;
        //LastAddress = (int)Address;
    }
    
    public Buffer(int size)
    {
        buffer = new byte[size];
    }
}
